package com.itheima.youdianda.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.BaseContext;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.dto.MyArticleDto;
import com.itheima.youdianda.dto.MyArticleListDto;
import com.itheima.youdianda.mapper.UserMapper;
import com.itheima.youdianda.pojo.YddUser;
import com.itheima.youdianda.pojo.YddUserData;
import com.itheima.youdianda.pojo.YddArticle;
import com.itheima.youdianda.service.UserService;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddUserDataService;
import com.itheima.youdianda.service.YddUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, YddUserData> implements UserService {
    @Autowired
    private YddArticleService yddArticleService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private YddUserDataService yddUserDataService;
    //循环依赖
//    @Autowired
//    private YddUserService yddUserService;

    @Override
    public MyArticleDto myArticle(String limit, String page) {
        Page<YddUserData> pageInfo = new Page<>(Convert.toLong(page), Convert.toLong(limit));
        LambdaQueryWrapper<YddUserData> lqw = new LambdaQueryWrapper<>();
//        String id = BaseContext.getId();
        Object id = request.getSession().getAttribute("userId");
        lqw.eq(YddUserData::getUserId, Convert.toInt(id));
        //测试
//        lqw.eq(YddUserData::getUserId, 136);
        page(pageInfo, lqw);

        MyArticleDto myArticleDto = new MyArticleDto();
        if (pageInfo.getRecords() == null || pageInfo.getRecords().size() == 0) {
            myArticleDto.setCount(0);
            myArticleDto.setPageSize(Convert.toInt(limit));
            myArticleDto.setCurrentPage(Convert.toInt(page));
            myArticleDto.setTotalPages(1);
            myArticleDto.setData(null);
            return myArticleDto;
        }

        //获取文章信息
        List<YddArticle> yddArticles = pageInfo.getRecords().stream().map(record -> {
            LambdaQueryWrapper<YddArticle> articleLqw = new LambdaQueryWrapper<>();
            articleLqw.eq(YddArticle::getId, record.getArticleId());
            return yddArticleService.getOne(articleLqw);
        }).collect(Collectors.toList());

        myArticleDto.setCount(Convert.toInt(pageInfo.getTotal()));
        myArticleDto.setPageSize(Convert.toInt(limit));
        myArticleDto.setCurrentPage(Convert.toInt(page));
        myArticleDto.setTotalPages(Convert.toInt(pageInfo.getTotal()) / Convert.toInt(limit) + 1);
        myArticleDto.setData(yddArticles);

        return myArticleDto;
    }

    @Override
    public MyArticleListDto userDataList(String type, String page, String limit) {
        List<MyArticleDto> list = new ArrayList<>();

        Page<YddUserData> pageInfo = new Page<>(Convert.toLong(page), Convert.toLong(limit));
        LambdaQueryWrapper<YddUserData> lqw = new LambdaQueryWrapper<>();
//        String id = BaseContext.getId();
        Object id = request.getSession().getAttribute("userId");
        //测试
//        lqw.eq(YddUserData::getUserId, 136);
        lqw.eq(YddUserData::getUserId, Convert.toInt(id));
        lqw.eq(YddUserData::getType, Convert.toInt(type));
        page(pageInfo, lqw);

        MyArticleDto myArticleDto = new MyArticleDto();
        MyArticleListDto myArticleListDto = new MyArticleListDto();
        if (pageInfo.getRecords() == null || pageInfo.getRecords().size() == 0) {
            myArticleDto.setCount(0);
            myArticleDto.setPageSize(Convert.toInt(limit));
            myArticleDto.setCurrentPage(Convert.toInt(page));
            myArticleDto.setTotalPages(1);
            myArticleDto.setData(null);
            list.add(myArticleDto);
            myArticleListDto.setList(list);
            return myArticleListDto;
        }

        //获取文章信息
        List<YddArticle> yddArticles = pageInfo.getRecords().stream().map(record -> {
            LambdaQueryWrapper<YddArticle> articleLqw = new LambdaQueryWrapper<>();
            articleLqw.eq(YddArticle::getId, record.getArticleId());
            return yddArticleService.getOne(articleLqw);
        }).collect(Collectors.toList());

        myArticleDto.setCount(Convert.toInt(pageInfo.getTotal()));
        myArticleDto.setPageSize(Convert.toInt(limit));
        myArticleDto.setCurrentPage(Convert.toInt(page));
        myArticleDto.setTotalPages(Convert.toInt(pageInfo.getTotal()) / Convert.toInt(limit) + 1);
        myArticleDto.setData(yddArticles);
        list.add(myArticleDto);
        myArticleListDto.setList(list);
        return myArticleListDto;
    }

    @Override
    public R addArticle(YddArticle yddArticle) {
        //添加我的文章
        //补全数据  userId
        Object id = request.getSession().getAttribute("userId");
        yddArticle.setUserId((Integer) id);
        yddArticleService.save(yddArticle);
       return R.success("保存成功");
    }

    @Override
    public R userDataHandle(Map<String, String> map) {
        /*
        点赞是通过map来接收的
         */
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        YddUserData yddUserData = new YddUserData();

        Integer articleId = Integer.valueOf(map.get("article_id"));
        String action = map.get("action");
        Integer type = Integer.valueOf(map.get("type"));
        yddUserData.setUserId(userId);
        yddUserData.setArticleId(articleId);
        yddUserData.setType(type);
        if ("add".equals(action)) {
            //如果接收到是add
            yddUserDataService.save(yddUserData);
        }

        if ("del".equals(action)) {
            LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(YddUserData::getType, articleId);
            yddUserDataService.remove(wrapper);
        }
        return R.success("操作成功");
    }

    @Override
    public R delArticle(String id) {
        //删除我的文章  遇到删除三思而行
        yddArticleService.removeById(id);
        return R.success("删除成功");
    }

//    @Override
//    public R getUserInfo() {
//
//    }
}
